using System.Web.Security;
using System.Configuration.Provider;
using System.Collections.Specialized;
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Diagnostics;
using System.Web;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Web.Configuration;

namespace ExtraClub.Domain {
    public sealed class ExtraDbMembershipProvider : SqlMembershipProvider {
        #region DATA
        private static char[] randChars = "a0bcde1fghij2klmno3pqrst4uvwxy5zABCD6EFGHI7JKLMN8OPQRS9TUVWXYZ".ToCharArray();
        #endregion

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
            //bool signedIn = HttpContext.Current.User.Identity.IsAuthenticated;
            //isApproved = !signedIn;
            MembershipUser mu = base.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
            //if (status == MembershipCreateStatus.Success) {
            //    string role = "DR";
            //    if (signedIn){
            //        string[] roles = Roles.GetRolesForUser();
            //        switch (roles[0]) {
            //            case "SR":
            //                role = "XR";
            //                break;
            //            case "XR":
            //                throw new Exception("You cannot create a new Club under your current account. First Log out");
            //            case "CR":
            //                throw new Exception("You cannot create a new Club under your current account. First Log out");
            //            case "DR":
            //                throw new Exception("You cannot create a new Club under your current account. First Log out");
            //        }
            //    }
            //    Roles.AddUserToRole(mu.UserName, role);
            //}
            return mu;
        }
        public override string GeneratePassword() {
            if (MinRequiredNonAlphanumericCharacters == 0) {
                RNGCryptoServiceProvider rcsp = new RNGCryptoServiceProvider();

                //Always generate at least 14 characters in the random password
                int desiredLength = MinRequiredPasswordLength < 14 ? 14 : MinRequiredPasswordLength;
                byte[] randBytes = new byte[desiredLength];
                char[] convertedResult = new char[desiredLength];

                //First get some random values
                rcsp.GetBytes(randBytes);

                //Then convert these values into characters
                for (int i = 0; i < desiredLength; i++) {
                    int indexOffset = ((int)randBytes[i]) % randChars.Length;
                    convertedResult[i] = randChars[indexOffset];
                }
                return new String(convertedResult);
            } else {
                return base.GeneratePassword();
            }
        }
    }
}
